/****************************************************************************
**
** Copyright (C) 2009-2010 Mist Poryvaev.
** All rights reserved.
** Contact: Mist Poryvaev (mist.poryvaev@gmail.com)
**
** This file is part of the RailWorm project.
**
****************************************************************************/

#include "railwormsplash.h"

RailWormSplash * RailWormSplash::g_Instance = 0;

bool SplashEventHandler::eventFilter(QObject *_obj, QEvent *_ev) {
	if (_ev->type() == QEvent::MouseButtonPress) {
		return true;
	}
	if (_ev->type() == QEvent::MouseButtonDblClick) {
		return true;
	}
	return QObject::eventFilter(_obj, _ev);
}

RailWormSplash * RailWormSplash::i() {
	if (g_Instance == 0) {
		g_Instance = new RailWormSplash();
	}
	if (g_Instance->m_MainWindow) {
		QPoint _mw_pos = g_Instance->m_MainWindow->mapToGlobal(QPoint(0, 0));
		g_Instance->move(
				_mw_pos.x() + (g_Instance->m_MainWindow->width() - g_Instance->width()) / 2,
				_mw_pos.y() + (g_Instance->m_MainWindow->height() - g_Instance->height()) / 2
				);
	}
	return g_Instance;
}

RailWormSplash::RailWormSplash(): QSplashScreen(QPixmap(), Qt::WindowStaysOnTopHint) {
	m_MainWindow = 0;

	QPixmap p(460, 120);
	p.fill(Qt::white);
	QPainter pnt(&p);
	QPen _pen(QColor(220, 220, 255, 255));
	_pen.setWidth(5);
	pnt.setPen(_pen);
	pnt.drawRoundedRect(0, 0, p.width() - 1, p.height() - 1, 10, 10, Qt::AbsoluteSize);
	setPixmap(p);
	installEventFilter(new SplashEventHandler());
}

void RailWormSplash::SetMainWindow(QWidget * _w) {
	m_MainWindow = _w;
}
